<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 警告模块 -->
      <el-alert
        show-icon
        title="注意，只允许为第三级分类设置相关参数"
        type="warning"
      >
      </el-alert>
      <!-- 选择商品分类模块 -->
      <div class="kind">
        选择商品分类：<el-cascader
          v-model="value"
          :options="options"
          :props="props"
          @change="handleChange"
        ></el-cascader>
      </div>
      <!-- 动态静态参数 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 动态参数 -->
        <el-tab-pane label="动态参数" name="many">
          <el-button
            :disabled="isDisable"
            size="mini"
            type="primary"
            @click="addManyParams"
            >添加参数</el-button
          >
          <!-- 动态参数表格 -->
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="expand">
              <!-- 展开列 -->
              <template slot-scope="scope">
                <el-tag
                  :key="tag"
                  v-for="tag in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="editParams(scope.row)"
                  >编辑</el-button
                >
                <el-button size="mini" type="danger" icon="el-icon-delete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 静态属性 -->
        <el-tab-pane label="静态属性" name="only">
          <el-button
            :disabled="isDisable"
            size="mini"
            type="primary"
            @click="addOnlyParams"
            >添加属性</el-button
          >
          <!-- 静态属性表格 -->
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="">
                <el-button size="mini" type="primary" icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-button size="mini" type="danger" icon="el-icon-delete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 添加动态和静态属性的弹出对话框 -->
    <el-dialog
      :title="'添加' + title"
      :visible.sync="showAddParams"
      width="50%"
      @close="closeDialog"
    >
      <el-form
        :model="addParamsForm"
        :rules="addRules"
        ref="ruleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item :label="title" prop="attr_name">
          <el-input v-model="addParamsForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddParams = false">取 消</el-button>
        <el-button type="primary" @click="addParamsOk">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出对话框 -->
    <el-dialog
      :title="'编辑' + title"
      :visible.sync="showEditParams"
      width="50%"
      @close="closeDialog"
    >
      <el-form
        :model="editParamsForm"
        :rules="addRules"
        ref="ruleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item :label="title" prop="attr_name">
          <el-input v-model="editParamsForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEditParams = false">取 消</el-button>
        <el-button type="primary" @click="editOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: [],
      options: [],
      // 显示级联选择器进行的配置
      props: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      // 商品列表
      goodsList: [],
      // 当前被激活的是哪一个
      activeName: "many",
      // 动态表格绑定的数据
      tableData: [],
      // 控制按钮是否禁用
      // isDisable:true
      // 分类的长度
      length: 0,
      // 被选中的分类
      changeCate: [],
      // 控制显示添加静态动态参数的弹出框
      showAddParams: false,
      // 添加动态静态属性时的验证规则
      addRules: {
        attr_name: [
          { required: true, message: "请输入内容", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      // 添加静态动态属性所绑定的值
      addParamsForm: {
        attr_name: "",
      },
      // 控制编辑对话框是否弹出
      showEditParams: false,
      // 编辑对话框动态绑定的值
      editParamsForm: {
        attr_name: "",
      },
      // 控制标签和文本输入框那个显示
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("categories");
      if (res.meta.status !== 200) {
        return this.$message.error("获取参数列表失败");
      } else {
        this.goodsList = res.data;
        console.log(this.goodsList);
        this.options = res.data;
      }
    },
    // 当选择分类发生变化时
    handleChange(info) {
      console.log(info);
      if (info.length !== 3) {
        this.value = [];
        this.length = 0;
        this.tableData = [];
      } else {
        this.getParams();
      }
    },
    // tab被选中时
    handleClick(tab, event) {
      // console.log(tab);
      console.log(this.activeName);
      this.getParams();
    },
    // 获取参数数据
    async getParams() {
      // 获取数据
      const { data: res } = await this.$http.get(
        "categories/" + this.value[2] + "/attributes",
        { params: { sel: this.activeName } }
      );
      // console.log(res);
      this.length = 3;
      if (res.meta.status !== 200) {
        // return this.$message.error('获取参数信息失败')
      }
      this.tableData = res.data;
      res.data.forEach(item => {
        item.attr_vals = item.attr_vals ? item.attr_vals.split(" ") : [];
        item.inputVisible=false
        item.inputValue=''
      });
      console.log(res);
      console.log(this.tableData);
      // console.log(this.activeName);
    },
    // 点击添加动态参数
    addManyParams() {
      this.showAddParams = true;
    },
    // 点击添加静态属性
    addOnlyParams() {
      this.showAddParams = true;
    },
    // 关闭添加静态动态属性的弹出框进行的操作
    closeDialog() {
      this.$refs.ruleFormRef.resetFields();
    },
    // 点击确定添加动态静态属性
    async addParamsOk() {
      const { data: res } = await this.$http.post(
        "categories/" + this.value[2] + "/attributes",
        {
          attr_name: this.addParamsForm.attr_name,
          attr_sel: this.activeName,
        }
      );
      if (res.meta.status !== 201) {
        return this.$message.error("添加" + this.title + "失败");
      }
      this.$message.success("添加" + this.title + "成功");
      this.getParams();
      this.showAddParams = false;
    },
    // 点击编辑进行编辑
    editParams(row) {
      this.editParamsForm.attr_name = row.attr_name;
      this.showEditParams = true;
      console.log(row);
    },
    // 确认编辑成功
    editOk() {},
    handleClose(tag) {
      console.log(tag);
    },
    // 添加标签按下了回车 失去焦点
    handleInputConfirm(row){
      // console.log('按下回车');
      //  row.inputVisible = false;
      //   row.inputValue = '';
    },
    showInput(row){
      console.log(row);
      row.inputVisible = true;
      console.log(row);
      // this.$nextTick(_ => {
      //     this.$refs.saveTagInput.$refs.input.focus();
      //   });
    }
  },
  computed: {
    isDisable() {
      if (this.length === 3) {
        return false;
      } else {
        return true;
      }
    },
    title() {
      if (this.activeName === "only") {
        return "静态属性";
      } else if (this.activeName === "many") {
        return "动态参数";
      }
    },
    
  },
};
</script>

<style scoped>
.kind {
  margin-top: 15px;
}
.el-tag {
  margin: 5px;
}
.input-new-tag{
  width: 120px;
}
</style>